/* 
 * ProjectFile: message-broker\src\utils\tools.js
 * Created: lijianwei 2022-06-02 23:17:37
 * -----
 * LastModified: lijianwei 2023-01-10 10:48:45
 * -----
 * Copyright (c) 2016-2023 Rongwen Tech Ltd.
 * -----
 */

import { merge } from "merge-anything"

/**
 * 格式化时间
 * @param {*} time 时间戳
 * @param {*} format 
 * @returns string
 */
export function formatDate(time, format = 'YY-MM-DD hh:mm:ss') {
    let date = new Date(time);

    let year = date.getFullYear(),
        month = date.getMonth() + 1, // 月份是从0开始的
        day = date.getDate(),
        hour = date.getHours(),
        min = date.getMinutes(),
        sec = date.getSeconds();
    let preArr = Array.apply(null, Array(10)).map(function (elem, index) {
        return '0' + index;
    })

    let newTime = format.replace(/YY/g, year)
        .replace(/MM/g, preArr[month] || month)
        .replace(/DD/g, preArr[day] || day)
        .replace(/hh/g, preArr[hour] || hour)
        .replace(/mm/g, preArr[min] || min)
        .replace(/ss/g, preArr[sec] || sec);

    return newTime;
}

export const toMerge = merge